package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsMchntAuthCtrlInfoMapper;
import cc.rengu.igas.cops.common.entity.CopsMchntAuthCtrlInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import com.alibaba.fastjson.JSON;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author keyi
 * @date 2020/3/21.
 */
public class CopsMchntAuthCtrlInfoMapperImpl implements CopsMchntAuthCtrlInfoMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public CopsMchntAuthCtrlInfo selectMchtnAuthCtrlInfoByPriKey(CopsMchntAuthCtrlInfo info) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntAuthCtrlInfoByPrimaryKey");

        int recode = dbsUtil.dbsExecuteSql(sqlStr, info, info);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("未查询到数据，instId:<{}>,ctrlType:{}>,mchntNo:<{}>,termNo:<{}>,txnNum:<{}>,bizType:<{}>",
                    info.getInstId(), info.getCtrlType(), info.getMchntNo(), info.getTermNo(), info.getTxnNum(), info.getBizType());
            return null;
        } else if (Database.DBS_SUCCESS != 0) {
            rglog.info("查询失败，instId:<{}>,ctrlType:{}>,mchntNo:<{}>,termNo:<{}>,txnNum:<{}>,bizType:<{}>,recode:<{}>",
                    info.getInstId(), info.getCtrlType(), info.getMchntNo(), info.getTermNo(), info.getTxnNum(), info.getBizType(), recode);
            return null;
        }

        return info;
    }

    /**
     * 根据非业务主键查询商户业务权限控制信息
     *
     * @param instId 多法人标识
     * @param id     非业务主键
     * @return 商户业务权限控制信息
     * @throws Exception 异常
     */
    @Override
    public CopsMchntAuthCtrlInfo selectMchntAuthCtrlInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntAuthCtrlInfoById");
        CopsMchntAuthCtrlInfo copsMchntAuthCtrlInfo = new CopsMchntAuthCtrlInfo();
        copsMchntAuthCtrlInfo.setInstId(instId);
        copsMchntAuthCtrlInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, copsMchntAuthCtrlInfo, copsMchntAuthCtrlInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户业务权限控制信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return copsMchntAuthCtrlInfo;
    }

    /**
     * 根据商户号查询商户终端业务权限控制信息
     *
     * @param instId  多法人标识
     * @param mchntNo 商户号
     * @return
     * @throws Exception
     */
    @Override
    public List<CopsMchntAuthCtrlInfo> selectMchntAuthCtrlInfoByMchntNo(String instId, String ctrlType, String termNo, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCopsMchntAuthCtrlInfoByMchntNo");
        CopsMchntAuthCtrlInfo copsMchntAuthCtrlInfo = new CopsMchntAuthCtrlInfo();
        copsMchntAuthCtrlInfo.setInstId(instId);
        copsMchntAuthCtrlInfo.setCtrlType(ctrlType);
        copsMchntAuthCtrlInfo.setTermNo(termNo);
        copsMchntAuthCtrlInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, copsMchntAuthCtrlInfo, CopsMchntAuthCtrlInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据控制主体获取业务权限控制信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (CopsMchntAuthCtrlInfo) item).collect(Collectors.toList());
    }

    /**
     * 新增商户业务权限控制表
     *
     * @param copsMchntAuthCtrlInfo
     * @return
     * @throws Exception
     */
    @Override
    public int insertMchntAuthInfo(CopsMchntAuthCtrlInfo copsMchntAuthCtrlInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCopsMchntAuthInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, copsMchntAuthCtrlInfo, null);
        if (0 != iReturnCode) {
            rglog.error("新增业务权限控制表失败,", JSON.toJSONString(copsMchntAuthCtrlInfo));
        }
        return iReturnCode;
    }

}
